package ezy.lite.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Set;

public class Prefs { 
	private static Preference _prefs;

	public static void init(Context context) {
		_prefs = new Preference(context, context.getPackageName());
	}
	public static SharedPreferences prefs() {
		return _prefs.prefs();
	} 

	public static void apply(String key, boolean value) {
        _prefs.apply(key, value);
	}
	public static void apply(String key, int value) {
        _prefs.apply(key, value);
	}
    public static void apply(String key, long value) {
        _prefs.apply(key, value);
    }
    public static void apply(String key, float value) {
        _prefs.apply(key, value);
    }
    public static void apply(String key, String value) {
        _prefs.apply(key, value);
    }
    public static void apply(String key, Set<String> value) {
        _prefs.apply(key, value);
    }

	public static boolean get(String key, boolean def) {
		return _prefs.get(key, def);
	} 
	public static int get(String key, int def) {
		return _prefs.get(key, def);
	}
    public static long get(String key, long def) {
        return _prefs.get(key, def);
    }
    public static float get(String key, float def) {
        return _prefs.get(key, def);
    }
    public static String get(String key, String def) {
        return _prefs.get(key, def);
    }
    public static Set<String> get(String key, Set<String> def) {
        return _prefs.get(key, def);
    }

    public static void remove(String key) {
        _prefs.remove(key);
    }

    public static class Preference {
        private SharedPreferences _prefs;

        public Preference(Context context, String name) {
            _prefs = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        }
        public SharedPreferences prefs() {
            return _prefs;
        }

        public SharedPreferences.Editor edit() {
            return _prefs.edit();
        }

        public void remove(String key) {
            _prefs.edit().remove(key).apply();
        }

        public void apply(String key, String value) {
            SharedPreferences.Editor editor = _prefs.edit();
            editor.putString(key, value);
            editor.apply();
        }
        public void apply(String key, boolean value) {
            SharedPreferences.Editor editor = _prefs.edit();
            editor.putBoolean(key, value);
            editor.apply();
        }
        public void apply(String key, int value) {
            SharedPreferences.Editor editor = _prefs.edit();
            editor.putInt(key, value);
            editor.apply();
        }
        public void apply(String key, long value) {
            SharedPreferences.Editor editor = _prefs.edit();
            editor.putLong(key, value);
            editor.apply();
        }
        public void apply(String key, float value) {
            SharedPreferences.Editor editor = _prefs.edit();
            editor.putFloat(key, value);
            editor.apply();
        }
        public void apply(String key, Set<String> value) {
            SharedPreferences.Editor editor = _prefs.edit();
            editor.putStringSet(key, value);
            editor.apply();
        }

        public String get(String key, String def) {
            return _prefs.getString(key, def);
        }
        public boolean get(String key, boolean def) {
            return _prefs.getBoolean(key, def);
        }
        public int get(String key, int def) {
            return _prefs.getInt(key, def);
        }
        public long get(String key, long def) {
            return _prefs.getLong(key, def);
        }
        public float get(String key, float def) {
            return _prefs.getFloat(key, def);
        }
        public Set<String> get(String key, Set<String> def) {
            return _prefs.getStringSet(key, def);
        }
    }
}
